package link.jfire.socket.test;

import java.nio.charset.Charset;
import link.jfire.baseutil.simplelog.ConsoleLogFactory;
import link.jfire.baseutil.simplelog.Logger;
import link.jfire.jnet.common.InternalResult;
import link.jfire.jnet.common.buffer.ByteBuf;
import link.jfire.jnet.common.buffer.DirectByteBufPool;
import link.jfire.jnet.common.exception.SocketException;
import link.jfire.jnet.common.handler.DataHandler;

public class EchoHandler implements DataHandler
{
    private Logger logger = ConsoleLogFactory.getLogger(ConsoleLogFactory.DEBUG);
    
    @Override
    public Object handle(Object data, InternalResult entry) throws SocketException
    {
        ByteBuf<?> byteBuf = (ByteBuf<?>) data;
        logger.debug(byteBuf.toString());
        byteBuf.maskRead();
        logger.debug(byteBuf.toString(Charset.forName("utf8")));
        ByteBuf<?> result = DirectByteBufPool.getInstance().get(100);
        result.writeByte((byte) (byteBuf.resetRead().remainRead() + 1));
        logger.debug(result.toString());
        result.put(byteBuf);
        byteBuf.release();
        logger.debug(result.toString());
        return result;
    }
    
}
